#include "tsr.h"
#include "rectype.h"
#include "util.h"
#include "cnfield.h"
#include "nmfield.h"

#include "nmarrfield.h"
#include "nmarrfield.cpp"

#include "stdffield.h"
#include "stdffield.cpp"

using namespace std;
using namespace opsrc::stdf;

Tsr::Tsr()
{}

Tsr::Tsr(stdf_rec _stdfrec, int _reclen, std::ios::pos_type _fileRecFirstBytePosition, int _orderindex,  endian e, char* buf)
:RecBase(_stdfrec, _reclen, _fileRecFirstBytePosition, _orderindex), en(e) {
    if (buf!=0)
        readFields(buf);
}

Tsr::~Tsr() {
}


const StdfField<int> Tsr::getHeadNumber() const {
    return head_num;
}

void Tsr::setHeadNumber(StdfField<int> headnum) {
    head_num = headnum;
}

const StdfField<int> Tsr::getSiteNumber() const {
    return site_num;
}

void Tsr::setSiteNumber(StdfField<int> sitenum) {
    site_num = sitenum;
}

const StdfField<string> Tsr::getTestType() const {
    return test_typ;
}

void Tsr::setTestType(StdfField<string> testtyp) {
    test_typ = testtyp;
}

const StdfField<int> Tsr::getTestNumber() const {
    return test_num;
}

void Tsr::setTestNumber(StdfField<int> testnum) {
    test_num = testnum;
}

const StdfField<long> Tsr::getExecutionCount() const {
    return exec_cnt;
}

void Tsr::setExecutionCount(StdfField<long> execcnt) {
    exec_cnt = execcnt;
}

const StdfField<long> Tsr::getFailureCount() const {
    return fail_cnt;
}

void Tsr::setFailureCount(StdfField<long> failcnt) {
    fail_cnt = failcnt;
}


const StdfField<long> Tsr::getAlarmCount() const {
    return alrm_cnt;
}

void Tsr::setAlarmCount(StdfField<long> alrmcnt) {
    alrm_cnt = alrmcnt;
}

const StdfField<string> Tsr::getTestName() const {
    return test_nam;
}

void Tsr::setTestName(StdfField<string> testnam) {
    test_nam = testnam;
}

const StdfField<string> Tsr::getSequenceName() const {
    return seq_nam;
}

void Tsr::setSequenceName(StdfField<string> seqnam) {
    seq_nam = seqnam;
}

const StdfField<string> Tsr::getTestLabel() const {
    return test_lbl;
}


void Tsr::setTestLabel(StdfField<string> testlbl) {
    test_lbl = testlbl;
}

const StdfField<vector<bool> > Tsr::getOptionFlag() const {
    return opt_flag;
}

void Tsr::setOptionFlag(StdfField<vector<bool> > optflag) {
    opt_flag = optflag;
}

const StdfField<float> Tsr::getTestTime() const {
    return test_tim;
}

void Tsr::setTestTime(StdfField<float> testtim) {
    test_tim = testtim;
}

const StdfField<float> Tsr::getLowestTestValue() const {
    return test_min;
}

void Tsr::setLowestTestValue(StdfField<float> testmin) {
    test_min = testmin;
}


const StdfField<float> Tsr::getHighestTestValue() const {
    return test_max;
}

void Tsr::setHighestTestValue(StdfField<float> testmax) {
    test_max = testmax;
}

const StdfField<float> Tsr::getTestResultSumValue() const {
    return test_sum;
}

void Tsr::setTestResultSumValue(StdfField<float> testsum) {
    test_sum = testsum;
}

const StdfField<float> Tsr::getTestResultSquareValue() const {
    return test_sqrs;
}

void Tsr::setTestResultSquareValue(StdfField<float> testsqrs) {
    test_sqrs = testsqrs;
}

void Tsr::readFields(char *buf) {
#if _DEBUG
    cout << "stdfrec= TSR , filebytestartposition= " <<   getFileRecByteStartPosition() << ", recbytecount= " << reclen <<endl;
#endif

    int curpos = 0;
    // Read head number , 1 byte
    NmField<unsigned int, 1> headnum(buf+curpos, en);
    head_num.set(headnum.getData(), curpos, headnum.getByteCount(), headnum.getTotalByteCount());
    curpos += head_num.getTotalByteCount();

    // Read site num, 1 byte
    if (curpos < reclen-1){
        NmField<unsigned int, 1> sitenum(buf+curpos, en);
        site_num.set(sitenum.getData(), curpos, sitenum.getByteCount(), sitenum.getTotalByteCount());
        curpos += site_num.getTotalByteCount();
    }

    // Read test type, 1 byte
    if (curpos < reclen-1) {
        CnField<string,1> testtyp(buf+curpos, en);
        test_typ.set(testtyp.getData(), curpos, testtyp.getByteCount(), testtyp.getTotalByteCount());
        curpos += test_typ.getTotalByteCount();
    }

    // Read test number, 4 byte
    if (curpos < reclen-1) {
        NmField<long,4> testnum(buf+curpos, en);
        test_num.set(testnum.getData(), curpos, testnum.getByteCount(), testnum.getTotalByteCount());
        curpos += test_num.getTotalByteCount();
    }

    if (curpos < reclen -1) {
        NmField<long,4> execcnt(buf+curpos, en);
        exec_cnt.set(execcnt.getData(), curpos, execcnt.getByteCount(), execcnt.getTotalByteCount());
        curpos += exec_cnt.getTotalByteCount();
    }

    if (curpos < reclen-1) {
        NmField<long,4> failcnt(buf+curpos, en);
        fail_cnt.set(failcnt.getData(), curpos, failcnt.getByteCount(), failcnt.getTotalByteCount());
        curpos += fail_cnt.getTotalByteCount();
    }

    // Read alarm count, 4 byte
    if ( curpos < reclen -1) {
        NmField<long,4> alrmcnt(buf+curpos, en);
        alrm_cnt.set(alrmcnt.getData(), curpos, alrmcnt.getByteCount(), alrmcnt.getTotalByteCount());
        curpos += alrm_cnt.getTotalByteCount();
    }

    // Read test name
    if (curpos < reclen -1) {
        CnField<string> testnam(buf+curpos, en);
        test_nam.set(testnam.getData(), curpos, testnam.getByteCount(), testnam.getTotalByteCount());
        curpos += test_nam.getTotalByteCount();
    }
    // Read sequence name
    if (curpos < reclen-1) {
        CnField<string> seqnam(buf+curpos, en);
        seq_nam.set(seqnam.getData(), curpos, seqnam.getByteCount(), seqnam.getTotalByteCount());
        curpos += seq_nam.getTotalByteCount();
    }


    // Read test label
    if (curpos < reclen-1) {
        CnField<string> testlbl(buf+curpos, en);
        test_lbl.set(testlbl.getData(), curpos, testlbl.getByteCount(), testlbl.getTotalByteCount());
        curpos += test_lbl.getTotalByteCount();
    }


    if (curpos < reclen -1 )
    {
        // Read option label
        NmArrField<bool, 1 > optflag(buf+curpos, en ,1);
        opt_flag.set(optflag.getData(), curpos, optflag.getByteCount(), optflag.getTotalByteCount());

        if( optflag.at(2) != "1")
        {
            curpos += opt_flag.getTotalByteCount();
            // Read average test time
            NmField<float, 4> testtim(buf+curpos, en);
            test_tim.set(testtim.getData(), curpos, testtim.getByteCount(), testtim.getTotalByteCount());
        }

        if( optflag.at(0) != "1")
        {
            curpos += opt_flag.getTotalByteCount();
            // Read lowest test result
            NmField<float, 4> testmin(buf+curpos, en);
            test_min.set(testmin.getData(), curpos, testmin.getByteCount(), testmin.getTotalByteCount());
        }

        if( optflag.at(1) != "1")
        {
            curpos += test_min.getTotalByteCount();
            // Read highest test result
            NmField<float, 4> testmax(buf+curpos, en);
            test_max.set(testmax.getData(), curpos, testmax.getByteCount(), testmax.getTotalByteCount());
        }

        if( optflag.at(4) != "1")
        {
            curpos += test_max.getTotalByteCount();
            if (curpos > reclen -1) return ;
            // Read test result sum
            NmField<float, 4> testsum(buf+curpos, en);
            test_sum.set(testsum.getData(), curpos, testsum.getByteCount(), testsum.getTotalByteCount());
        }

        if( optflag.at(5) != "1")
        {
            curpos += test_sum.getTotalByteCount();
            if (curpos > reclen -1) return ;
            // Read sum of squares result
            NmField<float, 4> testsqrs(buf+curpos, en);
            test_sqrs.set(testsqrs.getData(), curpos, testsqrs.getByteCount(), testsqrs.getTotalByteCount());
        }
    }
}
